<template>
	<view class="auth">
		<image class="image" src="../../static/logo.png" mode=""></image>
		<view class="apply">
			申请获取以下权限
		</view>
		<view class="description">
			获取您的手机号,用于货品签收
		</view>
		<button type="primary" open-type="getPhoneNumber" style="margin-top: 30px;"
			@getphonenumber="getPhoneNumber">获取手机号</button>
	</view>
</template>

<script>
	import WXBizDataCrypt from '@/utils/WXBizDataCrypt.js'
	export default {
		data() {
			return {
				phoneCode: '',
				openId: '',
				unionid: '',
				session_key: '',
			}
		},
		onLoad(options) {
			this.getPhone()
			console.log(options);
			this.query = options.query
		},
		methods: {
			// 获取手机号权限
			getPhone() {
				uni.login({
					provider: 'weixin',
					success: (res) => {
						this.phoneCode = res.code
						this.$http('post', '/Base_Manage/Base_User/GetWxData?JsCode=' + this.phoneCode +
							'&type=psq').then(
							result => {
								if (result.Success) {
									let data = JSON.parse(JSON.parse(result.Data).data)
									console.log(data);
									this.openId = data.openid //openid 用户唯一标识  
									this.unionid = data.unionid //unionid 开放平台唯一标识
									this.session_key = data.session_key //session_key  会话密钥  
								}
							})
					}
				})
			},
			getPhoneNumber(e) {
				if (e.detail.errMsg.indexOf('fail') != -1) return
				let pc = new WXBizDataCrypt('wxfb29728d48534058', this.session_key);
				let data = pc.decryptData(e.detail.encryptedData, e.detail.iv);
				uni.setStorageSync('userInfo', {
					phone: data.phoneNumber
				})
				uni.redirectTo({
					url: '/pages/index/index?q=' + this.query
				})
			},
		}
	}
</script>

<style scoped>
	.auth {
		text-align: center;
		padding: 40rpx;
		padding-top: 80rpx
	}

	.image {
		width: 400rpx;
		height: 400rpx;
	}

	.apply {
		margin-top: 40rpx;
	}

	.description {
		margin: 40rpx 0;
		color: #999;
	}
</style>